<?php

namespace app\api\controller\v1;

use app\api\controller\Api;
use app\sns\validate\ValidataCommon;
use data\extend\ErrCode;
use data\extend\poster\Poster;
use data\service\CategoryService;
use data\service\ContentService;
use data\service\ImgCatService;
use data\service\ImgsService;
use data\service\StickersService;
use think\facade\Config;
use think\facade\Env;
use think\Request;

class Kpss extends Api{

    private $categoryService ;
    private $contentService ;

    private $imgCatService;
    private $imgsService;

    private $stickerService;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->categoryService = new CategoryService();
        $this->contentService = new ContentService();
        $this->imgCatService = new ImgCatService();
        $this->imgsService = new ImgsService();
        $this->stickerService = new StickersService();
    }

    /**
     * 不需要鉴权方法
     * index、save不需要鉴权
     * ['index','save']
     * 所有方法都不需要鉴权
     * [*]
     */
    protected $noAuth = ['searchContent','posterMaker','getPosterInfo','getTemplate','setTemplate'];
    /**
     * 需要登录方法
     * index、save需要登录方法
     * ['index','save']
     * 默认所有方法都不需要登录
     * [*]
     */
    protected $mustLogin = [];


    /**
     * @param Request $request
     */
    public function getCategory(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'cT' => 'require'
            ], $request->post('')); //参数验证
            $cT = $request->post('cT',0);
            $categoryLists = $this->categoryService->getCategorys(1,20,['ctype'=>$cT],"sortby DESC",'id,name,img,favs,hits');

            return self::returnMsg(ErrCode::$OK,'success',$categoryLists);
        }
    }

    public function updateCategoryInc(Request $request){
        if($request->isPost()){
            $categoryId = $request->post('categoryId',0);
            $type = $request->post('type','');
            $this->categoryService->fieldInc($categoryId,$type,1);
            return self::returnMsg(ErrCode::$OK,'success',[]);
        }

    }

    /**
     * @param Request $request
     */
    public function categoryDetail(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'catId' => 'require'
            ], $request->post('')); //参数验证
            $catId = $request->post('catId',0);
            $category = $this->categoryService->categoryDetail($catId,'id,name,img,favs,hits,desc');

            return self::returnMsg(200,'success',$category);
        }
    }

    public function searchContent(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'wd' => 'require'
            ], $request->post('')); //参数验证

            $wd = $request->post('wd',1);
            $page = $request->post('page',1);

            $contentLists = $this->contentService->getContent($page <= 0?1:$page,30,[['desc','like',"%".$wd."%"]],'id DESC','id,ptype,type,desc,xclass');

            return self::returnMsg(ErrCode::$OK,'success',$contentLists);
        }

    }

    /**
     * @param Request $request
     */
    public function getContent(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'catId' => 'require'
            ], $request->post('')); //参数验证

            $catId = $request->post('catId',1);
            $page = $request->post('page',1);

            $contentLists = $this->contentService->getContent($page <= 0?1:$page,30,['ptype'=>$catId],'id DESC','id,ptype,type,desc,xclass');

            return self::returnMsg(ErrCode::$OK,'success',$contentLists);
        }
    }


    public function getImgCat(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'cT' => 'require'
            ], $request->post('')); //参数验证

            $cT = $request->post('cT',0);

            $impsCat = $this->imgCatService->getImgCat(1,30,['ctype'=>$cT,'isshow'=>1],"sortby ASC",'id,name,sortby,ctype');

            return self::returnMsg(ErrCode::$OK,'success',$impsCat);
        }
    }


    public function getImgs(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'imgCatId' => 'require'
            ], $request->post('')); //参数验证

            $imgCatId = $request->post('imgCatId',0);
            $page = $request->post('page',1);
            $page = rand(1,20);
            $where = $imgCatId != 0 ? ['pcid'=>$imgCatId] : ['pcid'=>25];

            $contentLists = $this->imgsService->getImgsLists($page <= 0?1:$page,30,$where,'id DESC','id,cid,url,cdn,w,h,pcid');

            return self::returnMsg(ErrCode::$OK,'success',$contentLists);
        }
    }


    public function getPosterInfo(Request $request){
        if($request->isPost()){
            ValidataCommon::validateCheck([
                'contentId' => 'require',
                'picId' => 'require'
            ], $request->post('')); //参数验证
            $contentId = $request->post('contentId',0);
            $picId = $request->post('picId',0);

            $contentDetail = $this->contentService->contentDetail($contentId,'desc');

            if(empty($contentDetail)){
                return self::returnMsg(ErrCode::$EmptyData,'success',[]);
            }

            $imgDetail = $this->imgsService->imgDetail($picId,'cdn');

            if(empty($imgDetail)){
                return self::returnMsg(ErrCode::$EmptyData,'success',[]);
            }
            $weekarray = ["日","一","二","三","四","五","六"];

            $info = [
                'width' => "720px",
                'height' => "1280px",
                'background' => "#ffffff",
                'views' => [
                    [
                        'type' => 'image',
                        'url' => CDN_ROOT_WEB.'static/'.$imgDetail['cdn'],
                        'css' => [
                            'top' => '0px',
                            'left' => '0px',
                            'width' => '720px',
                            'height' => '1280px',
                        ]
                    ],
                    [
                        'type' => 'image',
                        'url' => CDN_ROOT_WEB.'images/templatex.png'.IMG_STYLE,
                        'css' => [
                            'top' => '0px',
                            'left' => '0px',
                            'width' => '377px',
                            'height' => '1280px',
                        ]
                    ],
                    [
                        'type' => 'image',
                        'url' => CDN_ROOT_WEB.'images/ftqr.png'.IMG_STYLE,
                        'css' => [
                            'bottom' => '0px',
                            'left' => '0px',
                            'width' => '720px',
                            'height' => '123px',
                        ]
                    ],
                    [
                        'type' => 'text',
                        'text' => date("Y年m月d日",time()),
                        'css' => [
                            'fontSize' => '25px',
                            'lineHeight' =>'27px',
                            'width' => '200px',
                            'height' => '27px',
                            'color' => '#ffffff',
                            'left' => '483px',
                            'top'=>'68px',
                            'textAlign'=>'right',
                        ]
                    ],
                    [
                        'type' => 'text',
                        'text' => "星期".$weekarray[date("w")],
                        'css' => [
                            'fontSize' => '25px',
                            'lineHeight' =>'27px',
                            'width' => '174px',
                            'height' => '27px',
                            'color' => '#ffffff',
                            'left' => '509px',
                            'top'=>'110px',
                            'textAlign'=>'right',
                        ]
                    ],
                    [
                        'type' => 'text',
                        'text' => '天气晴天',
                        'css' => [
                            'fontSize' => '25px',
                            'lineHeight' =>'45px',
                            'width' => '174px',
                            'height' => '27px',
                            'color' => '#ffffff',
                            'left' => '509px',
                            'top'=>'147px',
                            'textAlign'=>'right'
                        ]
                    ],
                    [
                        'type' => 'text',
                        'text' => $contentDetail['desc'],
                        'css' => [
                            'fontSize' => '30px',
                            'lineHeight' =>'45px',
                            'width' => '300px',
                            'height' => '390px',
                            'color' => '#ffffff',
                            'left' => '62px',
                            'top'=>'644px',
                            'textAlign'=>'left'
                        ]
                    ]
                ]

            ];
            return self::returnMsg(ErrCode::$OK,'success',$info);

        }
    }

    public function getStickers(Request $request){
        if($request->isPost()){
            $page = $request->post('page',1);
            $stickerLists = $this->stickerService->getStickersLists($page <= 0?1:$page,30,[],'id DESC','id,url');
            return self::returnMsg(ErrCode::$OK,'success',$stickerLists);
        }
    }

    public function posterMaker(Request $request){
        if($request->isGet()){
            //p($request->post(''));
            $config = config('poster.');


            Poster::setConfig($config);
            $res = Poster::make();
            p('<img src="data:image/png;base64,'.$res.'" />');
            //p('data:image/png;base64,'.$res);

        }

    }
    public function getTemplate(Request $request){
        if($request->isPost()){
            $config = Config::load('formats');
            //$changes = [":bgImage"=>"",':content' => "hahahahahah"];
            //$info = arrayFormats($config['formats'],$changes);
            return self::returnMsg(ErrCode::$OK,'success',$config['formats']);
        }
    }
    public function setTemplate(Request $request){
        if($request->isPost()){
            $config = Config::load('formats');

            $templateId = $request->post('templateId',0);
            $content = $request->post('content',"");
            $image = $request->post('image',"");

            $content = trim(mb_strimwidth($content, 0, 100,"…",'UTF-8'));

            $changes = [":bgImage"=>$image,':content' => $content];
            $info = arrayFormats($config['formats'][$templateId],$changes);
            return self::returnMsg(ErrCode::$OK,'success',$info);
        }
    }


}
